<?php
/**
 * Created by PhpStorm.
 * User: lizx
 * Date: 2018/4/19
 * Time: 15:01
 */

namespace app\admin\controller\v1;

use app\admin\controller\Fun;

/**
 * @title 后台-APP版本管理-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class AppVersion extends Auth
{

    /**
     * @title 添加app版本
     * @description
     * @author lizx
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:AppVersion desc:接口组(全局)
     * @param name:command type:string require:1 default: other:addVersion desc:添加版本信息
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:version_code type:string require:1 default: other: desc:版本号
     * @param name:version_name type:string require:1 default: other: desc:版本名称
     * @param name:app_sys type:string require:1 default: other: desc:操作系统1安卓2iOS
     * @param name:app_type type:string require:1 default: other: desc:app类型1捎货2捎货人
     * @param name:file_url type:string require:1 default: other: desc:安装包地址安卓必传
     * @param name:update_info type:string require:1 default: other: desc:更新内容
     * @param name:force_update type:string require:1 default: other: desc:是否强制更新1是
     *
     * @return data:[]
     */
    public function addVersion()
    {
        extract($this->param);
        if(empty($version_code ?? 0) || empty($version_name ?? 0) || empty($app_sys ?? 0) || empty($app_type ?? 0)){
            return Fun::error('999993');
        }
        if($app_sys == 1 && empty($file_url ?? '')){
            return Fun::error('999993');
        }

        try{
            \app\common\Model\AppVersion::create([
                'version_code' => $version_code,
                'version_name' => $version_name,
                'app_sys' => $app_sys,
                'app_type' => $app_type,
                'file_url' => $file_url ?? '',
                'update_info' => $update_info ?? '',
                'force_update' => $force_update ?? 0
            ]);
            return Fun::success([],'添加成功');
        }catch (\Exception $exception){
            return Fun::error('');
        }
    }

    /**
     * @title app版本列表
     * @description
     * @author lizx
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:AppVersion desc:接口组(全局)
     * @param name:command type:string require:1 default: other:appList desc:版本列表
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:app_sys type:string require:1 default: other: desc:操作系统1安卓2iOS
     * @param name:app_type type:string require:1 default: other: desc:app类型1捎货2捎货人
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     *
     * @return data:[]
     */
    public function appList()
    {
        $where = [];

        if(!empty($this->param['app_type'])){
            $where['app_type'] = $this->param['app_type'];
        }
        if(!empty($this->param['app_sys'])){
            $where['app_sys'] = $this->param['app_sys'];
        }

        $page = empty($this->param['page']) ? 1 : $this->param['page'];
        $page_size = empty($this->param['page_size']) ? 10 : $this->param['page_size'];
        $paginate = [
            'page' => $page,
        ];

        $list = \app\common\Model\AppVersion::where($where)
            ->order('version_code desc,id desc')
            ->paginate(intval($page_size),false,$paginate)->toArray();

        $info['total_num'] = $list['total'];
        $info['list'] = $list['data'];

        return Fun::success($info);
    }
}